package com.thoughtworks.android.androsphere.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.thoughtworks.android.androsphere.dao.model.ClientRegistration;

public class ClientRegistrationDao extends SimpleJdbcDaoSupport {
    private static final Logger logger = Logger.getLogger(ClientRegistrationDao.class);

    public List<ClientRegistration> getAll() {
        String sql = "SELECT * FROM CLIENTREGISTRATION";
        return getSimpleJdbcTemplate().query(sql, new ClientRegistrationRowMapper());
    }

    public int delete(String deviceId) {
        String sql = "DELETE FROM CLIENTREGISTRATION WHERE DEVICEID = ?";
        return getSimpleJdbcTemplate().update(sql, deviceId);
    }

    public void insert(ClientRegistration clientRegistration) {
        String sql = "insert into CLIENTREGISTRATION values (?, ?)";
        getSimpleJdbcTemplate().update(sql, clientRegistration.getDeviceId(), clientRegistration.getRegistrationId());
    }

    public ClientRegistration get(String deviceId) {
        String sql = "SELECT * FROM CLIENTREGISTRATION WHERE DEVICEID = :deviceId";
        SqlParameterSource namedParameters = new MapSqlParameterSource("deviceId", deviceId);
        final List clientRegistrationList = getSimpleJdbcTemplate().query(sql, new ClientRegistrationRowMapper(), namedParameters);

        if(clientRegistrationList.isEmpty()) return null;

        return (ClientRegistration) clientRegistrationList.get(0);
    }

    public ClientRegistration getByRegistrationId(String registrationId) {
        logger.debug("Inside getByRegistrationId : " + registrationId);

        String sql = "SELECT * FROM CLIENTREGISTRATION WHERE REGISTRATIONID = :registrationId";
        SqlParameterSource namedParameters = new MapSqlParameterSource("registrationId", registrationId);
        final List clientRegistrationList = getSimpleJdbcTemplate().query(sql, new ClientRegistrationRowMapper(), namedParameters);
        logger.debug("clientRegistrationList size :" + clientRegistrationList.size());
        if(clientRegistrationList.isEmpty()) return null;

        return (ClientRegistration) clientRegistrationList.get(0);
    }

    public void update(ClientRegistration clientRegistration) {
        String sql = "update CLIENTREGISTRATION set REGISTRATIONID = ? WHERE DEVICEID = ?";
        getSimpleJdbcTemplate().update(sql, clientRegistration.getRegistrationId(), clientRegistration.getDeviceId());
    }

    public int deleteByRegistrationId(String registrationId) {
        String sql = "DELETE FROM CLIENTREGISTRATION WHERE REGISTRATIONID = ?";
        return getSimpleJdbcTemplate().update(sql, registrationId);
    }

    protected static final class ClientRegistrationRowMapper implements RowMapper {
        public ClientRegistration mapRow(ResultSet rs, int rowNum) throws SQLException {
            return new ClientRegistration(rs.getString("DEVICEID"), rs.getString("REGISTRATIONID"));
        }
    }
}
